home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / morse / cw_rx / cw.c next >
C/C++ Source or Header  |  1994-10-02  |  9KB  |  276 lines

  1. /***************************************************************
  2.  *             CW.C                       *
  3.  *                                   *
  4.  *        OH3FG/KO4BC    oct. 1, 1994                *
  5.  *              slkhautaniem@tac.fi                            *
  6.  *                                                             *
  7.  ***************************************************************/
  8.  
  9.  
  10. #include <dos.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <stdio.h>
  14.                     /* Printer port has 5 inputs:    *
  15.                      * pins 10, 11, 12, 13, and 15    */
  16. #define MSK_15 0x08
  17. #define MSK_13 0x10
  18. #define MSK_12 0x20
  19. #define MSK_10 0x40
  20. #define MSK_11 0x80
  21.                     /* Definitions to make code    *
  22.                      * more readable        */
  23. #define KEYUP (inportb(PRN + 1) & MSK)
  24. #define KEYDOWN (!KEYUP)
  25. #define UPDATE_UNIT unit = (unit + i)/2
  26. #define SIDETONE_ON sound(400)
  27. #define SIDETONE_OFF nosound()
  28. #define OUT_WITH_KBD if (kbhit()) {SIDETONE_OFF; printf("%d\n",unit); exit(0);}
  29. #define CNT (sizeof(chrs)/sizeof(chrs[0]))
  30.  
  31. typedef struct
  32.     {
  33.         char cw_image;
  34.         char asc;
  35.     }
  36.     MRK;
  37.  
  38. /*             char        cw-image    sounds like    */
  39. MRK chrs[] =
  40.          {
  41.          {  5,'A'},        /* 101         dit-dah        */
  42.          { 24,'B'},        /* 11000     dah-dit-dit-dit */
  43.          { 26,'C'},        /* 11010     dah-dit-dah-dit */
  44.          { 12,'D'},        /* 1100     dah-dit-dit    */
  45.          {  2,'E'},        /* 10         dit        */
  46.          { 18,'F'},        /* 10010     dit-dit-dah-dit */
  47.          { 14,'G'},        /* 1110     dah-dah-dit    */
  48.          { 16,'H'},        /* 10000    dit-dit-dit-dit */
  49.          {  4,'I'},        /* 100,        dit-dit        */
  50.          { 23,'J'},        /* 1011     dit-dah-dah-dah */
  51.          { 13,'K'},        /* 1101        dah-dit-dah    */
  52.          { 20,'L'},        /* 10100    dit-dah-dit-dit */
  53.          {  7,'M'},        /* 111        dah-dah        */
  54.          {  6,'N'},        /* 110        dah-dit        */
  55.          { 15,'O'},        /* 1111        dah-dah-Σah    */
  56.          { 22,'P'},        /* 10110    dit-dah-dah-dit */
  57.          { 29,'Q'},        /* 11101    dah-dah-dit-dah */
  58.          { 10,'R'},        /* 1010        dit-dah-dit    */
  59.          {  8,'S'},        /* 1000        dit-dit-dit    */
  60.          {  3,'T'},        /* 11        dah        */
  61.          {  9,'U'},        /* 1001        dit-dit-dah    */
  62.          { 17,'V'},        /* 10001    dit-dit-dit-dah */
  63.          { 11,'W'},        /* 1011        dit-dah-dah    */
  64.          { 25,'X'},        /* 11001    dah-dit-dit-dah */
  65.          { 27,'Y'},        /* 11011    dah-dit-dah-dah */
  66.          { 28,'Z'},        /* 11100    dah-dah-dit-dit */
  67.          { 45,'Å'},             /* 101101       . - - . -       */
  68.          { 21,'Ä'},             /* 10101        . - . -         */
  69.          { 30,'Ö'},             /* 11110        - - - .         */
  70.          { 63,'0'},        /* 111111    - - - - -    */
  71.          { 47,'1'},        /* 101111    . - - - -    */
  72.          { 39,'2'},        /* 100111    . . - - -    */
  73.          { 35,'3'},        /* 100011    . . . - -    */
  74.          { 33,'4'},        /* 100001    . . . . -    */
  75.          { 32,'5'},        /* 100000    . . . . .    */
  76.          { 48,'6'},        /* 110000    - . . . .    */
  77.          { 56,'7'},        /* 111000    - - . . .    */
  78.          { 60,'8'},        /* 111100    - - - . .    */
  79.          { 62,'9'},        /* 111110    - - - - .    */
  80.          { 49,'='},        /* 110001    - . . . -    */
  81.          { 85,'.'},        /* 1010101    . - . - . -    */
  82.          {115,','},        /* 1110011    - - . . - -    */
  83.          { 50,'/'},        /* 110010    - . . - .    */
  84.          { 76,'?'},        /* 1001100    . . - - . .    */
  85.          { 42,'+'},        /* 101010    . - . - .    */
  86.          { 54,'\n'},        /* 110110    - . - - .    */
  87.                  { 34,'\b'},            /* 100010       . . . - .       */
  88.                  { 69,'@'}              /* 1000101      . . . - . -     */
  89.                  };
  90.  
  91. char usage[] =
  92.         " This program reads the MORSE -code given by cw-key\n"
  93.         " into a printer port of a PC -computer.\n"
  94.         " Following hookup works fine in authors computer:\n\n"
  95.         "         printer port\n"
  96.         "         pin 9                   o──┐\n"
  97.         "                                    │\n"
  98.         "                                    █ 1k\n"
  99.         "                                    │\n"
  100.         "         pin 10,11,12,13, or 15  o──┼─────o\n"
  101.         "                                    ╪ 1u   cw-key\n"
  102.         "         pin 25                  o──┴─────o\n\n"
  103.         " Usage: CW pin# port# [unit_value]\n\n"
  104.         " If your key is connected between pins 12 and 25 at LPT1, you should command\n\n"
  105.         "        CW 12 1 \n\n"
  106.         " The program exits by pressing any key while program is running.\n"
  107.         " At exit, it prints unit value you can give as third parameter next time\n"
  108.         " you run the program.\n"
  109.         "                         Oct. 2,1994 OH3FG/KO4BC";
  110.  
  111.  
  112. int unit = 200;            /* Default length of 'dit'    *
  113.                  * 200 suitable for fast AT's   */
  114. int space = 0;
  115. int PRN,MSK;
  116.  
  117. char chr(void);
  118. void neuvo(char *s);
  119.  
  120. main(int argc,char *argv[])
  121. {
  122.     int c;
  123.     unsigned int cc;
  124.     unsigned int far *f;
  125.  
  126. /*
  127.  * Parse the command line parameters
  128.  */
  129.  
  130.                                                 /* If pin number and    *
  131.                                                  * port not given       *
  132.                                                  * show help text       *
  133.                                                  * and exit             */
  134.  
  135.     if (argc < 3) neuvo(usage);
  136.  
  137.                                                 /* If optional unit     *
  138.                                                  * value given,         *
  139.                                                  * then use it.         */
  140.  
  141.     if ((c = atoi(argv[3])) != 0) unit = c;
  142.  
  143.                                                 /* Get the port number  */
  144.  
  145.     c = atoi(argv[2]);
  146.     f = MK_FP(0x40,6);
  147.     cc = *(f+c);
  148.     if (!cc) neuvo("No such port!");
  149.     PRN = cc;
  150.                                                 /* Pin, where key       *
  151.                                                  * connected, is also   *
  152.                                                  * needed               */
  153.  
  154.     c = atoi(argv[1]);
  155.     SIDETONE_OFF;
  156.     switch (c)
  157.     {
  158.     case 10 : MSK = MSK_10; break;
  159.     case 11 : MSK = MSK_11; break;
  160.     case 12 : MSK = MSK_12; break;
  161.     case 13 : MSK = MSK_13; break;
  162.     case 15 : MSK = MSK_15; break;
  163.     default : neuvo("Pin, where key connected, must be 10, 11, 12, 13, or 15.");
  164.     }
  165.                                                 /* Put voltage into     *
  166.                                                  * pullup resistor      */
  167.  
  168.     outportb(PRN,0x80);
  169.  
  170. /*
  171.  * Main loop of the program
  172.  */
  173.  
  174.     for (;;)
  175.     {
  176.     c=chr();
  177.     if (c=='\n') putch('\r');        /* If newline,        */
  178.         if (c) putch(c);                        /* print CR first,      */
  179.     }
  180. }
  181.  
  182. /*
  183.  * chr() function does almost all the dirty work
  184.  */
  185.  
  186. char chr()
  187. {
  188.    int i,                    /* Duration of elements */
  189.        mrk,                    /* Cw-image        */
  190.        c = 0;                    /* Character to return    */
  191.  
  192.    mrk = 1;                    /* Raise up the cw-image*
  193.                          * begin flag.        */
  194.  
  195.                         /* Wait 'till key down  *
  196.                          * or time of four    *
  197.                          * units.        */
  198.    for (i=0;KEYUP && (i<4*unit);i++) OUT_WITH_KBD;
  199.                         /* If pause longer than *
  200.                          * four units then make *
  201.                          * just one space.    */
  202.    if ((i>=4*unit) && space)
  203.    {
  204.       space = 0;                /* No more than 1 space */
  205.       return ' ';
  206.    }
  207.                         /* Get cw-image of char *
  208.                          * into mrk variable    */
  209.    while KEYUP OUT_WITH_KBD;
  210.    for (;;)                    /* When the key goes down...*/
  211.    {
  212.       SIDETONE_ON;                /*  put on the tone    */
  213.       mrk <<= 1;                /*  shift image left    */
  214.       for (i=0;KEYDOWN;i++) OUT_WITH_KBD;       /*  wait until key up   */
  215.  
  216.       SIDETONE_OFF;                /* Turn the tone off    */
  217.  
  218.       if (i>2*unit)                /* If keydown time was    *
  219.                          * longer than two    *
  220.                          * units...        */
  221.       {
  222.       mrk |= 1;                /*  raise LSB of image    */
  223.       i /= 3;                /*  and divide time by 3*/
  224.       }
  225.                         /* Else, leave i and    *
  226.                          * mrk variables alone    */
  227.  
  228.                         /* The unit variable is *
  229.                          * to be updated with i *
  230.                          * variable - the key-    *
  231.                          * down time.        */
  232.       UPDATE_UNIT;
  233.                         /* If keyup time longer *
  234.                          * than two units...    */
  235.  
  236.       for (i=0;KEYUP && (i<2*unit);i++) OUT_WITH_KBD;
  237.  
  238.       if (i >= 2*unit) break;            /*  cw-image of char is *
  239.                          *  ready, so go for    *
  240.                          *  printing it        */
  241.  
  242.       else UPDATE_UNIT;                /* Else, i has length    *
  243.                          * for a 'dit', so    *
  244.                          * update unit. And go, *
  245.                          * get next element.    */
  246.    }
  247.                         /* Search for a char of *
  248.                          * this cw-image.    */
  249.  
  250.    for (i=0;i<CNT;i++)
  251.    {
  252.     if (chrs[i].cw_image == mrk)
  253.     {
  254.         c =  chrs[i].asc;        /* And return it.    */
  255.         break;
  256.     }
  257.                         /* Return 0 if        *
  258.                          * not found.        */
  259.    }
  260.                         /* Get ready for a    *
  261.                          * space between words. */
  262.    switch (c)
  263.    {
  264.     case '\n':                /* No space after back- */
  265.     case '\b':break;            /*  space or newline    */
  266.     default:space = 1;
  267.    }
  268.    return c;
  269. }
  270.  
  271. void neuvo(char *s)
  272. {
  273.     puts(s);
  274.     exit(1);
  275. }
  276.